package com.naiterui.ehp.bs.cms.modules.devops.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.modules.devops.config.ScheduleServiceConfig;
import com.naiterui.ehp.bs.cms.modules.devops.config.ScheduleServiceProperties;
import com.naiterui.ehp.bs.cms.modules.devops.feign.ScheduleFeign;
import com.naiterui.ehp.bs.cms.modules.devops.vo.ScheduleJobVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 定时任务
 * 
 * @author yangfuya
 * @since 2022/4/1
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.DEVOPS})
@RestController
@RequiredArgsConstructor
@PreAuthorize("@p.check('devops:schedule')")
@RequestMapping("/devops/schedule")
public class ScheduleController {
    private final ScheduleFeign scheduleFeign;

    @SysLog("定时任务列表")
    @ApiOperation("定时任务列表")
    // @PreAuthorize("@p.check('devops:schedule:list')")
    @GetMapping("job/list")
    public List<ScheduleJobVO> listSchedule() throws BusinessException {
        return scheduleFeign.listSchedule();
    }

    @SysLog("新增定时任务")
    @ApiOperation("新增定时任务")
    // @PreAuthorize("@p.check('devops:schedule:add')")
    @PostMapping("/add")
    public void add(@RequestBody @Validated ScheduleJobVO scheduleJobVO) throws BusinessException {
        scheduleFeign.add(scheduleJobVO);
    }

    @SysLog("修改定时任务")
    @ApiOperation("修改定时任务")
    // @PreAuthorize("@p.check('devops:schedule:update')")
    @PostMapping("/update")
    public void update(@RequestBody @Validated ScheduleJobVO scheduleJobVO) throws BusinessException {
        scheduleFeign.update(scheduleJobVO);
    }

    @SysLog("新增定时任务服务列表")
    @ApiOperation("新增定时任务服务列表")
    // @PreAuthorize("@p.check('devops:schedule:service:list')")
    @GetMapping("/service/list")
    public List<ScheduleServiceConfig> serviceList() {
        ScheduleServiceProperties serviceProperties = new ScheduleServiceProperties();
        return serviceProperties.getConfigs();
    }

    @SysLog("取消定时任务")
    @ApiOperation("取消定时任务")
    // @PreAuthorize("@p.check('devops:schedule:cancel')")
    @GetMapping("/cancel")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "任务名", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "group", value = "任务组", required = true, dataType = "String", paramType = "query")})
    public void cancelJob(@RequestParam("name") String name, @RequestParam("group") String group)
        throws BusinessException {
        scheduleFeign.cancelJob(name, group);
    }

    @SysLog("暂停定时任务")
    @ApiOperation("暂停定时任务")
    // @PreAuthorize("@p.check('devops:schedule:stop')")
    @GetMapping("/stop")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "任务名", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "group", value = "任务组", required = true, dataType = "String", paramType = "query")})
    public void stopJob(@RequestParam("name") String name, @RequestParam("group") String group)
        throws BusinessException {
        scheduleFeign.stopJob(name, group);
    }

    @SysLog("恢复定时任务")
    @ApiOperation("恢复定时任务")
    // @PreAuthorize("@p.check('devops:schedule:restart')")
    @GetMapping("/restart")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "任务名", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "group", value = "任务组", required = true, dataType = "String", paramType = "query")})
    public void restartJob(@RequestParam("name") String name, @RequestParam("group") String group)
        throws BusinessException {
        scheduleFeign.restartJob(name, group);
    }

}
